package services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.Region;
import models.RegionLevel;
import models.RegionLevelRelation;
import models.RoleMenu;
import models.SystemMenu;
import models.mappers.RegionLevelMapper;
import models.mappers.RegionLevelRelationMapper;
import models.mappers.RoleMenuMapper;
import models.mappers.RoleUserMapper;
import vos.RegionLevelRelationVo;

public class RegionLevelRelationService {
	/**
	 * 获取选中城市等级的城市
	 *
	 * @Title: getSelectRegion 
	 * @param levelid 城市等级id
	 * @return
	 * @return: List<String>
	 */
	public static List<String> getSelectRegion(String levelid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelRelationMapper mapper = ss.getMapper(RegionLevelRelationMapper.class);
            return mapper.getSelectRegion(levelid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取第一层级地区
	 *
	 * @Title: getFRegion 
	 * @return
	 * @return: List<Region>
	 */
	public static List<Region> getFRegion() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelRelationMapper mapper = ss.getMapper(RegionLevelRelationMapper.class);
            return mapper.getFRegion();
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据pid获取地区
	 *
	 * @Title: getSRegions 
	 * @param id
	 * @return
	 * @return: List<SystemMenu>
	 */
	public static List<Region> getSRegions(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelRelationMapper mapper = ss.getMapper(RegionLevelRelationMapper.class);
            return mapper.getSRegions(id);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取城市等级
	 *
	 * @Title: getRegionLevel 
	 * @return
	 * @return: List<RegionLevel>
	 */
	public static List<RegionLevel> getRegionLevel() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelRelationMapper mapper = ss.getMapper(RegionLevelRelationMapper.class);
            return mapper.getRegionLevel();
        } finally {
            ss.close();
        }
	}
	/**
	 * 保存城市等级划分
	 *
	 * @Title: saveRegionLevelRelation 
	 * @param levelid 城市等级id
	 * @param smap 地区列表
	 * @return
	 * @return: boolean
	 */
	public static boolean saveRegionLevelRelation(String levelid, Map<String, List<RegionLevelRelation>> smap) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		//将现有城市等级的城市删除，重新保存
		try {
			RegionLevelRelationMapper mapper = ss.getMapper(RegionLevelRelationMapper.class);
			mapper.deleteRegionLevelRelation(levelid);
			if(smap.get("lists")!=null){
				mapper.saveRegionLevelRelation(smap);
			}
            ss.commit();
        }catch(Exception e){
        	ss.rollback();
        	e.printStackTrace();
        	return false;
        }finally {
            ss.close();
        }
		return true;
	}
	/**
	 * 根据城市等级统计地区
	 *
	 * @Title: getByLevel 
	 * @param id
	 * @return
	 * @return: long
	 */
	public static RegionLevelRelation getByLevel(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.getByLevel(id);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据城市id获取等级信息
	 *
	 * @Title: getByRegionId 
	 * @param id
	 * @return
	 * @return: RegionLevelRelationVo
	 */
	public static RegionLevelRelationVo getByRegionId(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.getByRegionId(id);
        } finally {
            ss.close();
        }
	}
}
